package com.example.big_job;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.big_job.DB.DBHelper;
//import com.example.big_job.user.UserManager;

public class LoginActivity extends AppCompatActivity {
    private Button B_login;
    private EditText ET_User;
    private EditText ET_Pwd;
    private Button B_register;




    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //找到控件
        B_login=findViewById(R.id.bt_1);
        B_register=findViewById(R.id.bt_2);
        ET_User=findViewById(R.id.et_1);
        ET_Pwd=findViewById(R.id.et_2);

        //跳转主页面
        B_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username=ET_User.getText().toString();
                String password=ET_Pwd.getText().toString();
                Intent intent = null;
                Log.i("TAG",username+"    "+password);
                DBHelper sqlite = new DBHelper(LoginActivity.this,1);
                SQLiteDatabase database =sqlite.getReadableDatabase();
                String sql="select * from user where username=? and password=?";
                Cursor cursor=database.rawQuery(sql, new String[]{username,password});
                //Cursor Rawcursor = database.rawQuery("select * from user where userName=?", new String[]{username});
                if(cursor.moveToFirst()==true){
                    Log.i("TAG","登录成功");
                    Toast.makeText(getApplicationContext(), "登陆成功！", Toast.LENGTH_SHORT).show();
                    intent=new Intent(LoginActivity.this,MeauActivity.class);
                    startActivity(intent);
                }

                else{
                    Log.i("TAG","登录失败");
                    Toast.makeText(getApplicationContext(), "用户名不存在或密码有误！", Toast.LENGTH_SHORT).show();

                }
            }
        });
        //跳转注册页面
        B_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = null;
                intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);

            }
        });

    }


}